package com.hhit.collaborative_learning.management;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimerTask;

import com.hhit.collaborative_learning.utils.DBAccess;

public class CancelTopTask extends TimerTask {
	public final int SCHEDULE_HOUR=0;
	public boolean isRunning=false;
	
	@Override
	public void run() {
		Calendar cal=Calendar.getInstance();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		if(!isRunning){
			isRunning=true;
			if(cal.get(Calendar.HOUR_OF_DAY)==SCHEDULE_HOUR){
				Connection conn = null;
				PreparedStatement ps = null;
				cal.add(Calendar.DATE, -1);
				String sql="update blog set isTop=0 where str_to_date(time,'%Y-%m-%d %H:%i:%s')<'"+sdf.format(cal.getTime())+" 00:00:00'";
				try {
					conn=DBAccess.getInstance().getConn();
					ps=conn.prepareStatement(sql);
					ps.executeUpdate();
				} catch (SQLException e) {
					e.printStackTrace();
					isRunning=false;
				} finally{
					DBAccess.getInstance().close(conn, ps, null);
					isRunning=false;
				}
				
			}
			isRunning=false;
		}

	}

}
